#pragma once
#include "structs.h"
#include  <list>
#include "TLUT.h"

class ImagePanel;
void CALLBACK TimerProc(HWND hwnd, UINT msg, UINT idTimer, DWORD dwTime);
void CALLBACK TimerProcLoadFiles(HWND hwnd, UINT msg, UINT idTimer, DWORD dwTime);
int getNumberImgInSeries(DICOMStruct * dicomStruct);
class Panel:	
	public QObject
{
public:
	bool isActive;
	TWLLUT wlLUT;
	int positionXprev;
	int positionYprev;
	int positionXprevMouseDown;
	int positionYprevMouseDown;
	static BOOLEAN TEN_BITS_STATE;
	static bool isGrayMonitor;
	static float gamma;
	//static char state;	
	Panel();	
	virtual void ViewOne(Panel *);
	int ShowPopup(int);
	//void doColorTable(RGBQUAD * , int , int , int);
	//void doColorTable_13(RGBQUAD_13 * , int , int , int);
	static void transformColors(DICOMStruct *,  TWLLUT & lut, unsigned char *);	
	static BOOLEAN isQuickly;
	//static BOOLEAN isUpdateQuickly;
	BOOLEAN verifyDicomStruct(DICOMStruct * dicomStruct,  BOOLEAN isOnlyChosen = false);
	virtual ~Panel();
	virtual void flushRepresentationParameters();
	void setRepresentationParameters(RepresentationParameters);	
	RepresentationParameters representationParameters;
	//QStringList pNamesStudies;
	//QStringList pNamesSeries;
	static BOOLEAN flushedAllStates;
	static void setClickedImagePanel(Panel *);
	static char * StudyPath;
	static QMap <QString, TPlaneInfo> MPRPlaneInfos;
	static QMap <QString, mpr::TMPRBuilder *> MPRBuilders;
	static QList<DICOMStruct *> dicomStructList;
	//static std::list<Panel *> activePanels; 
	static std::list<Panel *> getActivePanels(); 
	static std::list<Panel *> Panel::activePanelsPrint;
	//static std::list<Panel *> activeTopPanels; 
	static std::list<DICOMStruct *> dicomStructsToPrint; 
	static BOOLEAN LOADFROMXRAYFOLDER;
	static BOOLEAN shifted;
	static BOOLEAN isMovie;
	static BOOLEAN isViewOneStudy;
	static BOOLEAN isAutoFillTopPanels;
	static BOOLEAN isMove;
	static BOOLEAN isMoveMPR;
	static BOOLEAN isNormalMode;
	static BOOLEAN isHideMarks;
	static BOOLEAN isContrast;
	static BOOLEAN isChoseActive;
	static BOOLEAN isChoosenActive;
	static BOOLEAN isHideOverlay;
	static BOOLEAN isHideOverlayMPR;
	static BOOLEAN isShowRefLine;
	static BOOLEAN isShowRefLineAll;
	static BOOLEAN isSizeDist;
	static BOOLEAN isSetSizeDist;
	static BOOLEAN isMPRPLane;
	static BOOLEAN isText;
	static BOOLEAN isAngle;
	static BOOLEAN isCurve;
	static BOOLEAN isSpot;
	static BOOLEAN isDeleteMark;
	static BOOLEAN isNeedUpdateAllInformationPanels;
	static BOOLEAN viewOnlyChosenDicomStructs;
	static BOOLEAN viewOnlyUnChosenDicomStructs;
	static int filterType;
	static Panel * clickedPanel;
	static Panel * clickedImagePanel;
	static Panel * saveTopPanel;
	static Panel * clickedTopPanel;
	static Panel * movedImagePanel;
	static Panel * topPrintPanel;	
	static BOOLEAN VlButtonDownGlobal;	
	static BOOLEAN VrButtonDownGlobal;	
	static HWND MainhWndWindow;	
	static BOOLEAN isNeededUpdateImages;
	static MEMManager * mEMManager;
	QString indication;
	BOOLEAN isFrozen;
	int xPosition;
	int yPosition;
	int widthWindow;
	int heightWindow;
	//static jpeg_error_mgr jpegErr;
	//static jpeg_decompress_struct jpegInfo;
	void reCreateAllWnd();
	static void flushAllStates();
	void convertYBRFULLtoRGB(void *,void *, int, int);
	void goNextAllImages(int);
	void goPrevAllImages(int);
	void addDeleteChildrenToActiveList();
	void addDeleteToActiveList(Panel *);
	void deleteFromActiveList(Panel * );
	void deleteFromActiveTopList(Panel * );
	void addFirstTopPanelIfNothing();
	//void hooseActiveList();
	void addFirstPanelIfNothing(Panel *);
	BOOLEAN isExistActivePanel(Panel *);
	void swapPanelsInActiveList(Panel * , Panel * );
	static BOOLEAN isActivePanel(Panel *);
	static BOOLEAN isActiveTopPanel(Panel *);
	void clearActiveList();
	void sortActiveTopPanels();
	void sortActivePanels();
	void clearTopActiveList();
	void chooseActiveList();
	void doNormalizedImage(DICOMStruct * dicomStruct);
	virtual void repaintImage(int, int) = 0;
	virtual void repaintImage();
	virtual void repaintInformationPanel();
	//virtual void moveImage(int, int) = 0;
	virtual void moveImage(float, float) = 0;
	virtual void reScale(float, int, int) = 0;
	//virtual void reScale(float);
	virtual void turnVert();
	virtual void turnHor();
	virtual void reflect();
	virtual void rotateRight();
	virtual void rotateLeft();	
	void turnVertStruct(DICOMStruct *);
	void turnHorStruct(DICOMStruct *);
	void reflectStruct(DICOMStruct *);
	void rotateRightStruct(DICOMStruct *);
	void rotateLeftStruct(DICOMStruct *);
	void turnRotateReflectToZeroStruct(DICOMStruct *);
	virtual void tryAddDicomStruct();	
	virtual BOOLEAN addImagePanel(char *); 
	virtual BOOLEAN addImagePanel(DICOMStruct *); 
	virtual BOOLEAN haveAllChosen(); 
	//HDC myDC;
	virtual HDC getMyDC();
	HBITMAP myhBitmap;
	//virtual HBITMAP getMyDCHBITMAP();

	void updateMainWindow();
	void deletePanel(Panel *);
	HFONT setFont(HDC, char *, int);
	static Panel * activeTopTreePanel;
	static Panel * activePanel;
	static Panel * activePanelPrint;
	static DICOMFile * firstDicomFile;
	static DICOMFile * currentDicomFile;		
	static DICOMFile * lastDicomFile;
	static void setQuickly(int timeMsc);
	//void CALLBACK _export TimerProc(HWND hwnd, UINT msg, UINT idTimer, DWORD dwTime);
	static DICOMFile * firstDicomFileDir;
	static DICOMFile * currentDicomFileDir;		
	static DICOMFile * lastDicomFileDir;
	static Panel * firstTopPanel;	
	static Panel * lastTopPanel;
	static int numberOfVisiblePanels;		
	static HBRUSH brush_125_125_125;	
	static HBRUSH brush_0_255_255;	
	static HBRUSH brush_0_0_0;
	static HBRUSH brush_255_255_255;
	static HBRUSH brush_255_255_0;
	static HBRUSH brush_150_0_0;
	static HBRUSH brush_0_255_0;
	Panel * nextTopPanel;
	Panel * prevTopPanel;
	HWND MainhWnd;
	HWND bottomhWnd;	
	Panel(HINSTANCE, char *);
	Panel * first;
	Panel * last;
	Panel * next;
	Panel * parent;
	BOOLEAN isChosen;
	BOOLEAN isChangedStudy;
	BOOLEAN isChangedSeries;
	//BOOLEAN isTopOfTree;
	void addPanel(Panel *);
	void addDICOMFile(DICOMFile * );	
	static DICOMStruct * getNext();
	//static void loadAllFiles(char *, QStringList &);
	//static void addFile(char * file, char * param = "", char * filter = "");
	static void setFirst(DICOMStruct *);
	static void setCurrent(DICOMStruct *);
	static void deleteAllDICOMStruct(QString filter = QString(""));
	static void deleteAllDICOMSeries(QString series);
	static int getNumberImgInSeries(ImagePanel * imagePanel);
	void addDICOMFileDir(DICOMFile * );	
	static DICOMStruct * getNextDir();
	static void setFirstDir(DICOMStruct *);
	static void setCurrentDir(DICOMStruct *);
	Panel * getTopTree();
	Panel * getSeriesPanel();
	void SetChosenAll(BOOLEAN, int type);	
	void resetChosenGroups();
	int verifyWindow(DICOMStruct *, int);
	int verifyLevel(DICOMStruct *, int);
	protected:	
	BOOLEAN isActiveTree();	
	ATOM MyRegisterClass(HINSTANCE);
	void drawBorder(HDC);	
	virtual void on_wm_paint(HWND , UINT , WPARAM , LPARAM);
	virtual void on_wm_lrbuttondown(HWND , UINT , WPARAM , LPARAM);
	virtual void on_wm_create(HWND , UINT , WPARAM , LPARAM);
	virtual void on_wm_destroy(HWND , UINT , WPARAM , LPARAM);
	virtual void on_wm_mousemove(HWND , UINT , WPARAM , LPARAM);
	virtual void on_wm_lrbuttonup(HWND , UINT , WPARAM , LPARAM);
	static LRESULT CALLBACK WndProc(HWND , UINT , WPARAM , LPARAM);
	virtual LRESULT WndProc_(HWND , UINT , WPARAM , LPARAM);
	//static int getSizePixel (DICOMStruct * dicomStruct);
	void deleteFromActiveListPrint(Panel * panel);
	BOOLEAN isActivePanelPrint(Panel * panel);
	void clearActiveListPrint();
	void addFirstPanelIfNothingPrint(Panel * parentPanel);
	void addFirstTopPanelIfNothingPrint();
	BOOLEAN isExistActivePanelPrint(Panel * panel);
	void chooseActiveListPrint();
	void addDeleteChildrenToActiveListPrint();
	void addDeleteToActiveListPrint(Panel * panel);
	static void updateQListDicomStructs();
	static Panel * pPanel;
	HDC hdcExample;
	static HINSTANCE hInst;
	char * lpszClassName;
	public:
		RECT rectMainWindow;
		int DCWidthConst;
		int DCHeightConst;
		BOOLEAN isDCSizeConst;
		int getDCWidth();
		int getDCHeight();
		void GetClientRect(HWND, LPRECT);
		void saveToFile(QString fileName);
		HDC getDC();
		QImage getQImage();

		void NOTLine(HDC, int, int, int, int);
	//	void whiteAndBlackLine(HDC, int, int, int, int, int, int, COLORREF);
		void whiteAndBlackLine(HDC hdc, int X1Line, int Y1Line, int X2Line, int Y2Line, int width, int widthContur, COLORREF color, RECT rect , int type = PS_SOLID);
		//void whiteAndBlackLine(HDC hdc, int X1Line, int Y1Line, int X2Line, int Y2Line, int width, int widthContur, COLORREF color, RECT rect, int type);
		//void blackAndWhiteLine(HDC, int, int, int, int, int, int, COLORREF);
		//void blackAndWhiteLine(HDC hdc, int X1Line, int Y1Line, int X2Line, int Y2Line, int width, int widthContur, COLORREF color, int type = PS_SOLID);
		void blackAndWhiteLine(HDC hdc, int X1Line, int Y1Line, int X2Line, int Y2Line, int width, int widthContur, COLORREF color, RECT rect, int type= PS_SOLID);
		//void blackAndWhiteLine(HDC hdc, int X1Line, int Y1Line, int X2Line, int Y2Line, int width, int widthContur, COLORREF color, int type = PS_SOLID, RECT rect = (RECT) (NULL);
		void whiteAndBlackEllipse(HDC, int, int, int, int, int, int, COLORREF);
		void blackAndWhiteEllipse(HDC, int, int, int, int, int, int, COLORREF);	
		void DrawTextMy(HDC hdc, char * str, int cchText, RECT * rect, unsigned int format, int heightFont);
		virtual float getMaxRatio();
		int getHeightForText(HDC hdc, char * text, int heightMax, int side);
		void DrawTextH(HDC hdc, char * str, int cchText, RECT * rect, unsigned int format, int heightFont);

		QStringList getSeriesList();
		QStringList getStudyList();
		static std::list<Panel *> getActiveTopPanels();
		VOID WINAPI OnDrawItem(HWND hwnd, LPDRAWITEMSTRUCT lpdis);
		VOID WINAPI OnMeasureItem(HWND hwnd, LPMEASUREITEMSTRUCT lpmis);
};






